package com.java.onlineshop.mvc.handler;

import com.java.onlineshop.pojo.Category;
import com.java.onlineshop.pojo.Product;
import com.java.onlineshop.service.ICategoryService;
import com.java.onlineshop.service.IProductService;
import com.java.onlineshop.service.impl.CategoryServiceImpl;
import com.java.onlineshop.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;

/**
 * @program: onlineshop
 * @description: 商品代码逻辑
 * @author: ShuangChenYue
 * @create: 2021-06-05 13:44
 **/
public class ProductHandler
{
//==================================================查找并显示所有商品====================================================
    public void showProductList(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("进入显示所有商品界面");
        /*逻辑如下：
                1.首先去数据库中查询并返回所有的商品(product)
                2.将返回的所有商品存在List<Product>集合中
                3.存在req请求中而后转发至显示页面
         */
            IProductService productService = new ProductServiceImpl();
            List<Product> allProductList = productService.findAllProduct();
            req.setAttribute("allProductList",allProductList);
            req.getRequestDispatcher("/productList.jsp").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//=====================================================显示商品详情======================================================
    public void showProductDetail(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击某个商品来此方法");
            //点击至此的同时，会带着一个商品id过来，我们首先获取该商品id
            String productId = req.getParameter("id");
            int id = Integer.parseInt(productId);
            //然后调用根据id查找商品的方法，返回一个商品
            IProductService productService = new ProductServiceImpl();
            Product product = productService.findProductById(id);
            //返回之后将其存在request域中，转发至详情页面
            req.setAttribute("product",product);
            req.getRequestDispatcher("/productDetail.jsp").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//============================================显示登陆后的商品细节========================================================
    public void showLoginProductDetail(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击某个商品来此方法");
            //点击至此的同时，会带着一个商品id过来，我们首先获取该商品id
            String productId = req.getParameter("id");
            int id = Integer.parseInt(productId);
            //然后调用根据id查找商品的方法，返回一个商品
            IProductService productService = new ProductServiceImpl();
            Product product = productService.findProductById(id);
            //返回之后将其存在request域中，转发至详情页面
            req.setAttribute("product",product);
            req.getRequestDispatcher("/loginProductDetail.jsp").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

//====================================================显示查询的商品=====================================================
    public void showProductBySearch(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击搜索来到此方法");
            req.setCharacterEncoding("utf-8");
            resp.setCharacterEncoding("utf-8");
            IProductService productService = new ProductServiceImpl();
        /*逻辑如下：
            1.在此获取输入框中所输入的name值
            2.调用根据模糊查询name的方法，查询product，并返回集合
            3.将集合存入request域中
            4.请求转发至显示页面显示所有查询的结果
         */
            String name = req.getParameter("name");
            System.out.println(name);
            List<Product> productList = productService.findProductByLikeName(name);
            req.setAttribute("productList",productList);
            req.getRequestDispatcher("/showProductBySearch.jsp").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//====================================================XXXXXXXXXXXXX=====================================================
    public void showLoginProductBySearch(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击搜索来到此方法");
            req.setCharacterEncoding("utf-8");
            resp.setCharacterEncoding("utf-8");
            IProductService productService = new ProductServiceImpl();
        /*逻辑如下：
            1.在此获取输入框中所输入的name值
            2.调用根据模糊查询name的方法，查询product，并返回集合
            3.将集合存入request域中
            4.请求转发至显示页面显示所有查询的结果
         */
            String name = req.getParameter("name");
            System.out.println(name);
            List<Product> productList = productService.findProductByLikeName(name);
            req.setAttribute("productList",productList);
            req.getRequestDispatcher("/showLoginProductBySearch.jsp").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//===========================================根据商品分类id查找并返回集合=================================================
    public void showProductByCategoryId(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击商品分类来此");
            IProductService productService = new ProductServiceImpl();
            ICategoryService categoryService = new CategoryServiceImpl();
            String id = req.getParameter("id");
            int categoryid = Integer.parseInt(id);
            List<Product> productListByCategoryId = productService.findProductByCategoryId(categoryid);
            req.setAttribute("productListByCategoryId",productListByCategoryId);
            Category category = categoryService.findById(categoryid);
            req.setAttribute("category",category);
            req.getRequestDispatcher("/showProductByCategoryId.jsp").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//=================================================登陆之后的分类方法=====================================================
    public void showLoginProductByCategoryId(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击商品分类来此");
            IProductService productService = new ProductServiceImpl();
            ICategoryService categoryService = new CategoryServiceImpl();
            String id = req.getParameter("id");
            int categoryid = Integer.parseInt(id);
            List<Product> productListByCategoryId = productService.findProductByCategoryId(categoryid);
            req.setAttribute("productListByCategoryId",productListByCategoryId);
            Category category = categoryService.findById(categoryid);
            req.setAttribute("category",category);
            req.getRequestDispatcher("/showLoginProductByCategoryId.jsp").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//======================================================================================================================
}
